<?php
/**************************************************/
/* Programmer: Dimasd Widjaya                     */
/*       Date: 2/5/2013                           */
/*  Page Name: webservices.php                     */
/*                                                */
/**************************************************/

/**************************************************/
/* Collection of webservices and related utils    */
/*                                                */
/**************************************************/

/**************************************************/
/*   Date        Reviser       Revision           */
/* --------     ---------     ----------          */
/*                                                */
/*                                                */
/**************************************************/



$COMPUTER_NAME = $_SERVER['COMPUTERNAME'];

	// Server configuration
    if ($COMPUTER_NAME === 'CSWEB')
    {
        $SERVER_ADDRESS = 'csweb.studentnet.int';
        $SERVER_USERNAME = 'cs414';
        $SERVER_PASSWORD = 'pcc1234';
    }
    else if ($COMPUTER_NAME === 'PRAENOMEN')
    {
        $SERVER_ADDRESS = 'localhost';
        $SERVER_USERNAME = 'root';
        $SERVER_PASSWORD = 'test_system';
    }
    else if($COMPUTER_NAME === 'BB-PC')
    {
        $SERVER_ADDRESS = 'localhost';
        $SERVER_USERNAME = 'root';
        $SERVER_PASSWORD = 'google';
    }
    else
    {
        exit();
    }
	// Created by: Dimas Widjaya
	// Date: 2/9/2013
	//
	// Call a stored procedure
	// First variable will ALWAYS be the name of the procedure being called, the rest will be any parameters needed 
	// for the stored procedure
	function CallProcedure()
	{
		
	
		$mysqli    = new mysqli($GLOBALS['SERVER_ADDRESS'],$GLOBALS['SERVER_USERNAME'], $GLOBALS['SERVER_PASSWORD'], "cs414");
		$CallQuery = "call "; 
		
		$ResultArray = array();
		
		if ($mysqli->connect_errno) 
		{
			echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
		}
		
		$indexes = func_num_args();
		$arguments = func_get_args();
		
		$CallQuery .= ($arguments[0] . '(');
		
		for($index = 1; $index < $indexes; $index++)
		{
			if(gettype($arguments[$index]) == 'string')
				$CallQuery .= ("'" . $arguments[$index] . "'");
			else
				$CallQuery .= $arguments[$index];
				
			if($index != $indexes - 1)
				$CallQuery .= ', ';
		}
		
		$CallQuery .= ')';

		if(!($result = $mysqli->query($CallQuery)))
		{
			echo $mysqli->error;
			exit();
		}
		
		while($row = $result->fetch_assoc())
		{
			array_push($ResultArray, $row);
		}
				
		return $ResultArray;
	}
	
	
	// Created by: Dimas Widjaya
	// Date: 2/9/2013
	//
	// Get the information needed for user dashboard
	function Get_user_dashboard_info($UserID = null)
	{
		if(isset($_POST['Func']))
		{
			$UserID = $_POST['UserID'];
			echo json_encode(CallProcedure('get_user_dashboard_info', $UserID));
		}
		else 
			return CallProcedure('get_user_dashboard_info', $UserID);
			
	}
	
	
	// Created by: Dimas Widjaya
	// Date: 2/11/2013
	//
	// Get the list of all user types
	function Get_all_user_type()
	{
		if(isset($_POST['Func']))
		{
			echo json_encode(CallProcedure('get_all_user_type'));
		}
		else 
			return CallProcedure('get_all_user_type');
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/11/2013
	//
	// Get the list of all students
	function Get_all_students($CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			echo json_encode(CallProcedure('get_all_students_for_enroll', $CourseID));
		}
		else 
			return CallProcedure('get_all_students_for_enroll', $CourseID);
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/11/2013
	//
	// Get the list of all teachers
	function Get_all_teachers($CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			echo json_encode(CallProcedure('get_all_teachers', $CourseID));
		}
		else 
			return CallProcedure('get_all_teachers', $CourseID);
			
	}


	// Created by: Dimas Widjaya
	// Date: 2/12/2013
	//
	// Get the list of all active courses
	function Get_all_active_courses()
	{
		if(isset($_POST['Func']))
		{
			echo json_encode(CallProcedure('get_all_active_courses'));
		}
		else 
			return CallProcedure('get_all_active_courses');
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/12/2013
	//
	// Get the teacher that is teaching a particular class
	function Get_teacher_of_a_course($CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			echo json_encode(CallProcedure('get_teacher_of_a_course', $CourseID));
		}
		else 
			return CallProcedure('get_teacher_of_a_course', $CourseID);
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/12/2013
	//
	// Get all students that is taking a particular class
	function Get_students_of_a_course($CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			echo json_encode(CallProcedure('get_students_of_a_course', $CourseID));
		}
		else 
			return CallProcedure('get_students_of_a_course', $CourseID);
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/14/2013
	//
	// Enroll a user to a course
	function Enroll_user_to_a_course($UserID = null, $CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			$UserID = $_POST['UserID'];
			CallProcedure('enroll_user_to_a_course', $UserID, $CourseID);
			echo json_encode((object) null);
		}
		else 
			return CallProcedure('enroll_user_to_a_course', $UserID, $CourseID);
			
	}
	
	// Created by: Dimas Widjaya
	// Date: 2/15/2013
	//
	// Remove a user from a course
	function Remove_user_from_a_course($UserID = null, $CourseID = null)
	{
		if(isset($_POST['Func']))
		{
			$CourseID = $_POST['CourseID'];
			$UserID = $_POST['UserID'];
			CallProcedure('remove_user_from_a_course', $UserID, $CourseID);
			echo json_encode((object) null);
		}
		else 
			return CallProcedure('remove_user_from_a_course', $UserID, $CourseID);
			
	}
	
	
	// Created by: Dominique Alons
	// Date: 2/11/2013
	//
	// Get the courses that a particular teacher is teaching
	function Get_teacher_courses($UserID = null)
	{
		if(isset($_POST['Func']))
		{
			$UserID = $_POST['UserID'];
			echo json_encode(CallProcedure('teacher_courses', $UserID));
		}
		else 
			return CallProcedure('teacher_courses', $UserID);
			
	}

	// Created by: Scott Gildersleeve
	// Date: 2/14/2013
	//
	// Get the information needed for class listings
	function Get_class_info($UserID = null)
	{
		if(isset($_POST['Func']))
		{
			$UserID = $_POST['UserID'];
			echo json_encode(CallProcedure('pop_class_arr', $UserID));
		}
		else 
			return CallProcedure('pop_class_arr', $UserID);
			
	}
	
	// Get the name of the function being called by JQuery AJAX
	@ $function = $_POST['Func'];
	
	// Execute the function being called by JQuery AJAX
	@ call_user_func($function, "");
	
	
?>